﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Cipyar.Core.DomainObjects;
using KdbMailer;
using SevenZip;
using CompressionLevel = SevenZip.CompressionLevel;
using CompressionMethod = SevenZip.CompressionMethod;

namespace Cipyar.Core
{
    public class ConfigurationManager
    {
        public static ConfigurationManager GetConfigurationManager(string fileName)
        {
            var configurationManager = new ConfigurationManager();
            configurationManager.Settings = configurationManager.GetSettings(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
            return configurationManager;
        }

        public static ConfigurationManager GetDefaultConfigurationManager()
        {
            return GetConfigurationManager("DomainObjects\\Settings.xml");
        }

        public Settings Settings
        {
            get {
                return _settings;
            }
            set {
                _settings = value;
            }
        }

        public Settings GetSettings(string fileName)
        {
            var serializer = new XmlSerializer(typeof (Settings));
            return (Settings) serializer.Deserialize(new StreamReader(fileName));
        }

        public void SetEncryptionSettings(SevenZipCompressor compressor, Compression compression)
        {
            if (compression.Encryption.Enabled)
            {
                compressor.EncryptHeaders = compression.Encryption.EncryptFileName;
                compressor.ZipEncryptionMethod = UtilityMethods.ToEnum<ZipEncryptionMethod>(compression.Encryption.EncryptionMethod.ToString());
            }
        }

        public SevenZipCompressor GetSevenZipCompressor(Compression compression)
        {
            SevenZipBase.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, compression.SevenZipLibraryPath));
            var sevenZipCompressor = new SevenZipCompressor(Path.GetTempPath())
                                         {
                                             ArchiveFormat = UtilityMethods.ToEnum<OutArchiveFormat>(compression.CompressionType.ToString()),
                                             CompressionLevel = UtilityMethods.ToEnum<CompressionLevel>(compression.CompressionLevel.ToString()),
                                             CompressionMethod = UtilityMethods.ToEnum<CompressionMethod>(compression.CompressionMethod.ToString()),
                                             CompressionMode = CompressionMode.Create
                                         };

            return sevenZipCompressor;

        }

        public void CreateNewCompressedFileName()
        {
            CompressedFileName = Path.Combine(@"e:\", "test.7z");
            return;
            CompressedFileName = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("u").Replace(' ', '_'));
        }

        private string _compressedFileName;

        private string _password;
        private Settings _settings;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string CompressedFileName
        {
            get { return _compressedFileName; }
            set { _compressedFileName = value; }
        }
    }
}
